import 'package:get_storage/get_storage.dart';
import 'package:demo/tools/tools_enum.dart';

// 本地存储
class ToolsStorage {
  ToolsStorage._();
  static ToolsStorage? _singleton;
  factory ToolsStorage() => _singleton ??= ToolsStorage._();
  // 存储对象
  final GetStorage _storage = GetStorage();

  // token
  String token({String token = ''}) {
    // 类型
    String type = 'token';
    // 读取
    if (token.isEmpty) {
      return _storage.read(type) ?? '';
    }
    // 写入
    _storage.write(type, token);
    return token;
  }

  // status
  MiddleStatus status({MiddleStatus? value}) {
    // 类型
    String type = 'status';
    // 读取
    if (value == null) {
      String value = _storage.read(type) ?? '';
      return MiddleStatus.init(value);
    }
    // 写入
    _storage.write(type, value.value);
    if (MiddleStatus.login == value) {
      // 清空
      _storage.erase();
    }
    return value;
  }

  // 登录信息
  LocalUser local({LocalUser? value}) {
    // 类型
    String type = 'local';
    if (value != null) {
      // 写入
      _storage.write(type, value.toJson());
      // 返回
      return value;
    }
    // 读取
    Map<String, dynamic> data = _storage.read(type) ?? {};
    // 转换
    return LocalUser.fromJson(data);
  }

  // chat
  LocalChat chat({LocalChat? value}) {
    // 类型
    String type = 'chat';
    if (value != null) {
      // 写入
      _storage.write(type, value.toJson());
      // 返回
      return value;
    }
    // 读取
    Map<String, dynamic> data = _storage.read(type) ?? {};
    // 转换
    return LocalChat.fromJson(data);
  }
}

class LocalChat {
  String chatId;
  String portrait;
  String title;
  String nickname;
  ChatTalk chatTalk;

  LocalChat({
    required this.chatId,
    required this.portrait,
    required this.title,
    required this.nickname,
    required this.chatTalk,
  });

  factory LocalChat.fromJson(Map<String, dynamic> data) {
    return LocalChat(
      chatId: data['chatId'],
      portrait: data['portrait'],
      nickname: data['nickname'],
      title: data['title'],
      chatTalk: ChatTalk.init(data['chatTalk']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'chatId': chatId,
      'portrait': portrait,
      'title': title,
      'nickname': nickname,
      'chatTalk': chatTalk.value,
    };
  }
}

class LocalUser {
  String userId;
  String nickname;
  String portrait;
  String gender;
  String genderLabel;
  String phone;
  String userNo;
  String intro;
  String province;
  String city;
  String birthday;
  String privacyNo;
  String privacyPhone;
  String privacyScan;
  String privacyCard;
  String privacyGroup;
  String pass;

  LocalUser(
    this.userId,
    this.nickname,
    this.portrait,
    this.gender,
    this.genderLabel,
    this.phone,
    this.userNo,
    this.intro,
    this.province,
    this.city,
    this.birthday,
    this.privacyNo,
    this.privacyPhone,
    this.privacyScan,
    this.privacyCard,
    this.privacyGroup,
    this.pass,
  );

  factory LocalUser.fromJson(Map<String, dynamic>? data) {
    return LocalUser(
      data?['userId'] ?? '',
      data?['nickname'] ?? '',
      data?['portrait'] ?? '',
      data?['gender'] ?? '',
      data?['gender'] == '1' ? '男' : '女',
      data?['phone'] ?? '',
      data?['userNo'] ?? '',
      data?['intro'] ?? '',
      data?['province'] ?? '',
      data?['city'] ?? '',
      data?['birthday'] ?? '',
      data?['privacyNo'] ?? '',
      data?['privacyPhone'] ?? '',
      data?['privacyScan'] ?? '',
      data?['privacyCard'] ?? '',
      data?['privacyGroup'] ?? '',
      data?['pass'] ?? 'N',
    );
  }

  factory LocalUser.init() {
    return LocalUser.fromJson({});
  }

  Map<String, dynamic> toJson() {
    return {
      'userId': userId,
      'nickname': nickname,
      'portrait': portrait,
      'gender': gender,
      'phone': phone,
      'userNo': userNo,
      'intro': intro,
      'province': province,
      'city': city,
      'birthday': birthday,
      'privacyNo': privacyNo,
      'privacyPhone': privacyPhone,
      'privacyScan': privacyScan,
      'privacyCard': privacyCard,
      'privacyGroup': privacyGroup,
      'pass': pass,
    };
  }
}
